<?php

namespace Home\Controller;
use EasyWeChat\Message\Text;
use Think\Controller;
use EasyWeChat\Foundation\Application;

/**
 * Class WeChatController
 * @package Home\Controller
 */
class WeChatController extends Controller {

    public function _initialize()
    {
        $options = [
            'app_id'  => 'wx737d2e33c724c999',         // AppID
            'secret'  => '3f62fcaba97e4fcfd3b4321f253c32ed',     // AppSecret
            'token'   => 'mABUye6rODMvnspbL7RGxcdhfaWNu4',          // Token
//            'aes_key' => '',// EncodingAESKey，安全模式下请一定要填写！！！
            'guzzle' => [
                'timeout' => 3.0, // 超时时间（秒）
//                'verify' => false, // 关掉 SSL 认证（强烈不建议！！！）
//                'verify' => getcwd().'/Data/cacert.pem',
            ]
        ];
        $this->Application = new Application($options);
    }


    public function index(){

        // 从项目实例中得到服务端应用实例。
        $server = $this->Application->server;
        $server->setMessageHandler(function ($message) {
            // $message->FromUserName // 用户的 openid
            // 当 $message->MsgType 为 event 时为事件
            if ($message->MsgType == 'event') {
                try{
                    $text = new Text();
//                $text->setAttribute('content', '未知消息');
//                    return $text->setAttribute('content', $message->Event);
                    return $this->dealEventMsg($message);
                }catch (\Exception $e){}
            }
        });
        $response = $server->serve();
        $response->send(); // Laravel 里请使用：return $response;
    }

    private function dealEventMsg($message){
        $text = new Text();
        switch ($message->Event) {
            case 'CLICK':
                if('V1001_GOOD' == $message->EventKey){
                    $text->setAttribute('content', '您好！overtrue。');
                }elseif ('V1001_TODAY_MUSIC' == $message->EventKey){
                    $text->setAttribute('content', <<<str
1、如果是从智联电视或盒子下载多唱K歌应用， <a href='http://develop.duochang.cc/user/home_ktv/Home/OnlineWebService/index?faq=faq2&noWx=1'>点击进入帮助链接</a>；
2、如果是点歌机用户，遇到软件、硬件故障，<a href='http://develop.duochang.cc/user/home_ktv/Home/OnlineWebService/index?faq=faq1&noWx=1'>点击进入帮助链接</a>；
str
                    );
                }
                if($text->getAttribute('content')){
                    $result = $text;
                    break;
                }
            default:
//                $text->setAttribute('content', '未知消息');
                $text->setAttribute('content', json_encode($message));
                $result = $text;
                break;
        }
        return $result;
    }


    /**
     * @return mixed
     */
    private function getReplyModule(){
        return $this->Application->reply;
    }


    /**
     * 获取菜单组件
     * @return \EasyWeChat\Menu\Menu
     */
    private function getMenuModule(){
        return $this->Application->menu;
    }

    /**
     * 创建菜单
     */
    public function createMenus(){

        $buttons = [
            [
                "type" => "click",
                "name" => "在线客服",
                "key"  => "V1001_TODAY_MUSIC"
            ],
            [
                "name"       => "菜单",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "在线客服1",
                        "url"  => "http://develop.duochang.cc/user/home_ktv/Home/OnlineWebService/index?faq=faq2&noWx=1"
                    ],
                    [
                        "type" => "view",
                        "name" => "在线客服2",
                        "url"  => "http://develop.duochang.cc/user/home_ktv/Home/OnlineWebService/index?faq=faq2&noWx=2"
                    ],
                    [
                        "type" => "click",
                        "name" => "赞一下我们",
                        "key" => "V1001_GOOD"
                    ],
                ],
            ],
        ];
        $result = $this->getMenuModule()->add($buttons);
        var_dump($result);
    }



}
